package com.mjfeed.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @description 商品
 * @author zhaominglei
 * @date 2014年9月21日
 */
@Entity
@Table(name="feed_product")
public class Product extends BaseEntity implements Serializable {
	private static final long serialVersionUID = -3118789079416725037L;
	public static final String PRODUCT_SN_SEPARATOR = ";";
	
	/** 已分配库存 */
	private Integer allocatedStock;
	/** 成本价 */
    private BigDecimal cost;
    /** 全称 */
    private String fullName;
    /** 点击数 */
    private Long hits;
    /** 展示图片 */
    private String image;
    /** 介绍 */
    private String introduction;
    /** 是否为赠品 */
    private Boolean isGift;
    /** 是否列出 */
    private Boolean isList;
    /** 是否上架 */
    private Boolean isMarketable;
    /** 是否置顶 */
    private Boolean isTop;
    /** 搜索关键词 */
    private String keyword;
    /** 市场价 */
    private BigDecimal marketPrice;
    /** 备注 */
    private String memo;
    /** 月点击数 */
    private Long monthHits;
    /** 月点击数更新日期 */
    private java.util.Date monthHitsDate;
    /** 月销量 */
    private Long monthSales;
    /** 月销量更新日期 */
    private java.util.Date monthSalesDate;
    /** 名称 */
    private String name;
    /** 描述 */
    private String description;
    /** 赠送积分 */
    private Long point;
    /** 销售价 */
    private BigDecimal price;
    /** 销量 */
    private Long sales;
    /** 评分 */
    private Float score;
    /** 评分数 */
    private Long scoreCount;
    /** 页面描述 */
    private String seoDescription;
    /** 页面关键词 */
    private String seoKeywords;
    /** 页面标题 */
    private String seoTitle;
    /** 编号 */
    private String sn;
    /** 库存 */
    private Integer stock;
    /** 库存备注 */
    private String stockMemo;
    /** 总评分 */
    private Long totalScore;
    /** 单位 */
    private String unit;
    /** 周点击数 */
    private Long weekHits;
    /** 周点击数更新日期 */
    private Date weekHitsDate;
    /** 周销量 */
    private Long weekSales;
    /** 周销量更新日期 */
    private Date weekSalesDate;
    /** 重量 */
    private Integer weight;
    /** 所有重量商品编号用;分隔 **/
	private String allWeightSn;
	/** 推荐配件商品编号用;分隔 **/
	private String recommendPartsSn;
	/** 推荐组合商品编号用;分隔 **/
	private String recommendGroupSn;

    /** 商品分类 */
	private ProductCategory productCategory;
	
	/** 商品图片 */
	private List<ProductImage> productImages = new ArrayList<ProductImage>();
	
	/** 购物车明细 */
	private Set<CartItem> cartItems = new HashSet<CartItem>();

	/** 订单明细 */
	private Set<OrderItem> orderItems = new HashSet<OrderItem>();
	
	/** 发货单明细  */
	private Set<ShippingItem> shippingItems = new HashSet<ShippingItem>();
	
	/** 收藏的会员 */
	private Set<Member> favoriteMembers = new HashSet<Member>();  
	
	/** 评论 */
	private Set<Comment> comments = new HashSet<Comment>();
	
	/** 浏览日志*/
    private Set<BrowseLog> browseLogs = new HashSet<BrowseLog>();
	
	@Column(name="allocated_stock")
	public Integer getAllocatedStock() {
		return allocatedStock;
	}

	public void setAllocatedStock(Integer allocatedStock) {
		this.allocatedStock = allocatedStock;
	}

	public BigDecimal getCost() {
		return cost;
	}

	public void setCost(BigDecimal cost) {
		this.cost = cost;
	}

	@Column(name="full_name")
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public Long getHits() {
		return hits;
	}

	public void setHits(Long hits) {
		this.hits = hits;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	@Column(name="introduction", columnDefinition="LONGTEXT")
	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

	@Column(name="is_gift", columnDefinition="BIT")
	public Boolean getIsGift() {
		return isGift;
	}

	public void setIsGift(Boolean isGift) {
		this.isGift = isGift;
	}
	
	@Column(name="is_list", columnDefinition="BIT")
	public Boolean getIsList() {
		return isList;
	}

	public void setIsList(Boolean isList) {
		this.isList = isList;
	}
	@Column(name="is_marketable", columnDefinition="BIT")
	public Boolean getIsMarketable() {
		return isMarketable;
	}

	public void setIsMarketable(Boolean isMarketable) {
		this.isMarketable = isMarketable;
	}
	
	@Column(name="is_top", columnDefinition="BIT")
	public Boolean getIsTop() {
		return isTop;
	}

	public void setIsTop(Boolean isTop) {
		this.isTop = isTop;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	@Column(name="market_price")
	public BigDecimal getMarketPrice() {
		return marketPrice;
	}

	public void setMarketPrice(BigDecimal marketPrice) {
		this.marketPrice = marketPrice;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	@Column(name="month_hits")
	public Long getMonthHits() {
		return monthHits;
	}

	public void setMonthHits(Long monthHits) {
		this.monthHits = monthHits;
	}
	
	@Column(name="month_hits_date")
	public Date getMonthHitsDate() {
		return monthHitsDate;
	}

	public void setMonthHitsDate(Date monthHitsDate) {
		this.monthHitsDate = monthHitsDate;
	}
	
	@Column(name="month_sales")
	public Long getMonthSales() {
		return monthSales;
	}

	public void setMonthSales(Long monthSales) {
		this.monthSales = monthSales;
	}

	@Column(name="month_sales_date")
	public Date getMonthSalesDate() {
		return monthSalesDate;
	}

	public void setMonthSalesDate(Date monthSalesDate) {
		this.monthSalesDate = monthSalesDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getPoint() {
		return point;
	}

	public void setPoint(Long point) {
		this.point = point;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public Long getSales() {
		return sales;
	}

	public void setSales(Long sales) {
		this.sales = sales;
	}

	public Float getScore() {
		return score;
	}

	public void setScore(Float score) {
		this.score = score;
	}
	
	@Column(name="score_count")
	public Long getScoreCount() {
		return scoreCount;
	}

	public void setScoreCount(Long scoreCount) {
		this.scoreCount = scoreCount;
	}

	@Column(name="seo_description")
	public String getSeoDescription() {
		return seoDescription;
	}

	public void setSeoDescription(String seoDescription) {
		this.seoDescription = seoDescription;
	}

	@Column(name="seo_keywords")
	public String getSeoKeywords() {
		return seoKeywords;
	}

	public void setSeoKeywords(String seoKeywords) {
		this.seoKeywords = seoKeywords;
	}

	@Column(name="seo_title")
	public String getSeoTitle() {
		return seoTitle;
	}

	public void setSeoTitle(String seoTitle) {
		this.seoTitle = seoTitle;
	}

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public Integer getStock() {
		return stock;
	}

	public void setStock(Integer stock) {
		this.stock = stock;
	}

	@Column(name="stock_memo")
	public String getStockMemo() {
		return stockMemo;
	}

	public void setStockMemo(String stockMemo) {
		this.stockMemo = stockMemo;
	}

	@Column(name="total_score")
	public Long getTotalScore() {
		return totalScore;
	}

	public void setTotalScore(Long totalScore) {
		this.totalScore = totalScore;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	@Column(name="week_hits")
	public Long getWeekHits() {
		return weekHits;
	}

	public void setWeekHits(Long weekHits) {
		this.weekHits = weekHits;
	}

	@Column(name="week_hits_date")
	public Date getWeekHitsDate() {
		return weekHitsDate;
	}

	public void setWeekHitsDate(Date weekHitsDate) {
		this.weekHitsDate = weekHitsDate;
	}

	@Column(name="week_sales")
	public Long getWeekSales() {
		return weekSales;
	}

	public void setWeekSales(Long weekSales) {
		this.weekSales = weekSales;
	}

	@Column(name="week_sales_date")
	public Date getWeekSalesDate() {
		return weekSalesDate;
	}

	public void setWeekSalesDate(Date weekSalesDate) {
		this.weekSalesDate = weekSalesDate;
	}

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	@Column(name="all_weight_sn")
	public String getAllWeightSn() {
		return allWeightSn;
	}

	public void setAllWeightSn(String allWeightSn) {
		this.allWeightSn = allWeightSn;
	}

	@Column(name="recommend_parts_sn")
	public String getRecommendPartsSn() {
		return recommendPartsSn;
	}

	public void setRecommendPartsSn(String recommendPartsSn) {
		this.recommendPartsSn = recommendPartsSn;
	}
	
	@Column(name="recommend_group_sn")
	public String getRecommendGroupSn() {
		return recommendGroupSn;
	}

	public void setRecommendGroupSn(String recommendGroupSn) {
		this.recommendGroupSn = recommendGroupSn;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="product_category_id")
	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	@ElementCollection
	@CollectionTable(name = "feed_product_product_image")
	public List<ProductImage> getProductImages() {
		return productImages;
	}

	public void setProductImages(List<ProductImage> productImages) {
		this.productImages = productImages;
	}
	
	@OneToMany(mappedBy="product", fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
	public Set<CartItem> getCartItems() {
		return cartItems;
	}

	public void setCartItems(Set<CartItem> cartItems) {
		this.cartItems = cartItems;
	}

	@OneToMany(mappedBy="product", fetch=FetchType.LAZY)
	public Set<OrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Set<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}
	
	@OneToMany(mappedBy="product", fetch=FetchType.LAZY)
	public Set<ShippingItem> getShippingItems() {
		return shippingItems;
	}

	public void setShippingItems(Set<ShippingItem> shippingItems) {
		this.shippingItems = shippingItems;
	}
	
	@ManyToMany(mappedBy="favoriteProducts", fetch=FetchType.LAZY)
	public Set<Member> getFavoriteMembers() {
		return favoriteMembers;
	}

	public void setFavoriteMembers(Set<Member> favoriteMembers) {
		this.favoriteMembers = favoriteMembers;
	}
	
	@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}
	
	@OneToMany(mappedBy="product", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<BrowseLog> getBrowseLogs() {
		return browseLogs;
	}

	public void setBrowseLogs(Set<BrowseLog> browseLogs) {
		this.browseLogs = browseLogs;
	}
	
	/**
	 * 获取缩略图
	 */
	@Transient
	public String getThumbnail() {
		if (getProductImages() != null && !getProductImages().isEmpty()) {
			return getProductImages().get(0).getThumbnail();
		}
		return null;
	}
}
